//
//  QJService.m
//  QJWork
//
//  Created by 高中雷 on 15/5/30.
//  Copyright (c) 2015年 高中雷. All rights reserved.
//

#import "QJService.h"
#import "QJDataHandle.h"

@implementation QJService
 static QJService * service = NULL;
+(QJService*)service{
    service = [[self alloc] init];
    return service;
}


-(id)init{
    if(self == [super init]){
        self.hostName = [self engineClass];
    }
    return self;
}

-(NSString *)engineClass{
    return @"";
}

#pragma mark - get请求
-(AFHTTPRequestOperation *)getWithUrl:(NSString *)url param:(id)param modelObject:(id)object withHandle:(QJBlockRequst)handler  failure:(failureBlock)failureHandler
{
     //拼接完整的URL
//     url =  [self compeletHttpUrlWithSubUrl:url];
    __weak typeof(QJDataHandle) *data = [QJDataHandle sharedInstance];
    return   [[QJHttpRequest http] requestWithURL:url requestHeader:@{}  params:[self getParameterWithParameter:param] image:nil httpMethod:@"GET" success:^(id responseObj) {
                            [data dataMapModel:responseObj modelObject:object handle:handler];
    } failure:^(NSError *error) {
        failureHandler(error);
    }];
}

#pragma mark - post请求
- (AFHTTPRequestOperation *)postWithUrl:(NSString *)url param:(id)param  modelObject:(id)object withHandle:(QJBlockRequst)handler   failure:(failureBlock)failureHandler
{
    
     __weak typeof(QJDataHandle) *data = [QJDataHandle sharedInstance];
  return   [[QJHttpRequest http] requestWithURL:url requestHeader:@{} params:[self getParameterWithParameter:param] image:nil httpMethod:@"POST" success:^(id responseObj) {
            [data dataMapModel:responseObj modelObject:object handle:handler];
    } failure:^(NSError *error) {
        failureHandler(error);
        
    }];
}

#pragma mark - post请求加图片 类型是数组
- (AFHTTPRequestOperation *)postWithUrl:(NSString *)url param:(id)param image:(NSArray *)imageArray modelObject:(id)object withHandle:(QJBlockRequst)handler   failure:(failureBlock)failureHandler
{
    __weak typeof(QJDataHandle) *data = [QJDataHandle sharedInstance];
    return [[QJHttpRequest http]requestWithURL:url requestHeader:@{} params:[self getParameterWithParameter:param] image:imageArray httpMethod:@"POSTIMAGE" success:^(id responseObj) {
        [data dataMapModel:responseObj modelObject:object handle:handler];
    } failure:^(NSError *error) {
//        [QJDataHandle error:error];
        failureHandler(error);
    }];
}


- (AFHTTPRequestOperation *)postDataWithUrl:(NSString *)url param:(id)param modelObject:(id)object withHandle:(QJBlockRequst)handler   failure:(failureBlock)failureHandler
{
    __weak typeof(QJDataHandle) *data = [QJDataHandle sharedInstance];
    return[[QJHttpRequest http]requestWithURL:url requestHeader:@{} params:param image:nil httpMethod:@"POSTDATA" success:^(id responseObj) {
        [data dataMapModel:responseObj modelObject:object handle:handler];
    } failure:^(NSError *error) {
         failureHandler(error);
    }];
}


//请求报文
-(NSMutableDictionary *)getParameterWithParameter:(NSDictionary *)nowParameter{
    
//    NSMutableDictionary * basicDict = (NSMutableDictionary *)[RJQuickMethod getParameterWithParameter:nowParameter];
    NSMutableDictionary *basicDict = nowParameter;
//    NSLog(@"dic..%@",basicDict);
    
    return basicDict;
}
#pragma mark - 拼接完整URL
-(NSString *)compeletHttpUrlWithSubUrl:(NSString *)subUrl{
    //拼接完整URL
    NSString *hostName = [[QJServiceHelper serviceHelper]getLocalHostNameWithClassName:self.hostName];
    NSString *url =[NSString stringWithFormat:@"%@%@", APPIP, subUrl];
    NSLog(@"url...%@",url);
    return url;
}

@end
